#!/usr/bin/env python
# -*- coding:utf-8 -*-

from flask_restful import Resource, reqparse
from flask import jsonify, request

from controller.ssoUserController import SsoUserController
from service.ssoUserService import SsoUserService
from utils import commons
from utils.loggings import loggings
from utils.response_code import RET, error_map_EN
from utils.validate_operate import verify_operator


class SsoUserOtherResource(Resource):

    @classmethod
    def sensitive_update(cls, user_id):
        if not user_id:
            return jsonify(code=RET.NODATA, message='primary key missed', error='primary key missed')

        parser = reqparse.RequestParser()
        parser.add_argument('password', location='form', required=False, help='password参数类型不正确或缺失')
        parser.add_argument('status', location='form', required=False, help='status参数类型不正确或缺失')
        parser.add_argument('is_deleted', location='form', required=False, help='is_deleted参数类型不正确或缺失')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)
        kwargs['user_id'] = user_id

        res = SsoUserController.update(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    @classmethod
    def sso_user_register(cls):
        """
            用户注册
        """
        parser = reqparse.RequestParser()
        parser.add_argument('account', location='form', required=True, help='account参数类型不正确或缺失')
        parser.add_argument('password', location='form', required=True, help='password参数类型不正确或缺失')
        parser.add_argument('email', location='form', required=True, help='email参数类型不正确或缺失')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        res = SsoUserService.register(**kwargs)
        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    # 用户登录
    @classmethod
    def sso_user_login(cls):
        # 接参数并校验
        parser = reqparse.RequestParser()
        parser.add_argument('account',  location='form', required=True, help='account参数类型不正确或缺失')
        parser.add_argument('password',  location='form', required=True, help='password参数类型不正确或缺失')
        parser.add_argument('verify_code_id',  location='form', required=True,
                            help='VerifyCodeID参数类型不正确或缺失')
        parser.add_argument('verify_code', location='form', required=True,
                            help='VerifyCode参数类型不正确或缺失')

        try:
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)
        except Exception as e:
            loggings.exception(1, e)
            return jsonify(code=RET.PARAMERR, message="参数类型不正确或缺失", error="参数类型不正确或缺失")

        from service.ssoUserService import SsoUserService
        res = SsoUserService.login(**kwargs)
        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    # 用户登出
    @classmethod
    def sso_user_logout(cls):
        from service.ssoUserService import SsoUserService
        res = SsoUserService.logout()
        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    # 发送邮箱验证码
    @classmethod
    def verify_email(cls):
        # 接参数并校验
        parser = reqparse.RequestParser()
        parser.add_argument('target_email', type=str, location='form', required=True,
                            help='account参数类型不正确或缺失')
        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)
        from service.ssoUserService import SsoUserService
        res = SsoUserService.verify_email(**kwargs)
        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    # 邮箱验证
    @classmethod
    def email_code_check(cls):
        # 接参数并校验
        parser = reqparse.RequestParser()
        parser.add_argument('mail_verify_id', location='form', required=True, help='mail_verify_id参数类型不正确或缺失')
        parser.add_argument('mail_verify_code', location='form', required=True,
                            help='mail_verify_code参数类型不正确或缺失')
        parser.add_argument('email', location='form', required=True, help='email参数类型不正确或缺失')
        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)
        from service.ssoUserService import SsoUserService
        res = SsoUserService.email_code_check(**kwargs)
        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    # 修改密码
    @classmethod
    def change_password(cls):
        # 接参数并校验
        parser = reqparse.RequestParser()
        parser.add_argument('user_id', location='form', required=True, help='user_id参数类型不正确或缺失')
        parser.add_argument('old_password', location='form', required=True, help='old_password参数类型不正确或缺失')
        parser.add_argument('new_password', location='form', required=True, help='new_password参数类型不正确或缺失')
        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        from service.ssoUserService import SsoUserService
        res = SsoUserService.change_password(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    # 用户忘记密码-根据用户名向邮箱发送验证码
    @classmethod
    def forget_password_send(cls):
        # 接参数并校验
        parser = reqparse.RequestParser()
        parser.add_argument('account', location='form', required=True, help='account参数类型不正确或缺失')
        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)
        from service.ssoUserService import SsoUserService
        res = SsoUserService.forget_password_send(**kwargs)
        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    # 用户忘记密码-验证邮箱验证码，找回密码
    @classmethod
    def forget_password_recover(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('account', location='form', required=True, help='account参数类型不正确或缺失')
        parser.add_argument('mail_verify_id', location='form', required=True, help='mail_verify_id参数类型不正确或缺失')
        parser.add_argument('mail_verify_code', location='form', required=True,
                            help='mail_verify_code参数类型不正确或缺失')
        parser.add_argument('email', location='form', required=True, help='email参数类型不正确或缺失')
        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)
        from service.ssoUserService import SsoUserService
        res = SsoUserService.forget_password_recover(**kwargs)
        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    @classmethod
    def verify_token(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('token', location='form', required=True, help='token参数类型不正确或缺失')
        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)
        from service.ssoUserService import SsoUserService
        res = SsoUserService.verify_token(**kwargs)
        return jsonify(code=res['code'], message=res['message'], data=res['data'])
